﻿using Microsoft.AspNetCore.Mvc;
using WebApiThird.Entity;
using WebApiThird.IService;
using WebApiThird.Model;
using WebApiThird.Service;

namespace WebApiThird.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class StudentController : ControllerBase
    {
        private readonly IStudentService studentService;
        //private readonly IStudentFileService studentFileService;

        public StudentController(IStudentService studentService) {
            this.studentService = studentService;
            //this.studentFileService = studentFileService;
        }

        [HttpPost]
        public async Task<bool> Add(StudentReq req)
        {
            return await studentService.Add(req);
        }

        [HttpGet("{id}")]
        public async Task<StudentRes?> Get([FromRoute]int id)
        {
            return await studentService!.GetStudent(id);
        }

        /// <summary>
        /// 封装内存缓存使用
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("ext/{id}")]
        public async Task<StudentRes?> GetExt(int id)
        {
            return await studentService!.GetStudentExt(id);
        }

        [HttpGet("Now")]
        [ResponseCache(Duration =33,Location = ResponseCacheLocation.Any)]
        public async Task<DateTime> GetDateTime()
        {
            return await Task.FromResult( DateTime.Now);
        }

        [HttpGet("fileCount")]
        public async Task<int> GetFileCount([FromServices] IStudentFileService studentFileService)
        {
            var count =  studentFileService.GetFileCount();
            return await Task.FromResult(count);
        }

        [HttpGet("students/{id}")]
        public async Task<List<Student>?> GetStudents([FromRoute]int id)
        {
           return await studentService.GetStudents(id);
        }

        /// <summary>
        ///  分布式缓存
        /// </summary>
        /// <returns></returns>
        [HttpGet("students/ext/{id}")]
        public async Task<List<Student>?> GetStudentsExt(int id)
        {
            return await studentService.GetStudentsExt(id);
        }

    }
}
